<template>
  <div>
    <el-card>
      <el-row :gutter="24" class="ma_5">
        <el-col :span="7">
          <el-row class="center_x aic">
            <el-col>
              <label>用户姓名:</label>
            </el-col>
            <el-col>
              <el-input v-model="username"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="7">
          <el-row class="center_x aic">
            <el-col>
              <label>实验编码:</label>
            </el-col>
            <el-col>
              <el-input v-model="experimentCode"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="6">
          <el-row class="center_x aic">
            <el-col>
              <label>实验名称:</label>
            </el-col>
            <el-col>
              <el-input v-model="experimentName"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="4">
          <el-row class="center_x aic">
            <el-col>
              <el-button @click="search()" type="primary">搜索</el-button>
            </el-col>
            <el-col>
              <el-button @click="reset()">重置</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row :gutter="24">
        <el-col :span="10">
          <strong>实验管理</strong>
        </el-col>
        <el-col :span="14" style="display: flex;flex-direction: row-reverse;">
          <el-button type="primary" @click="showAdd=true">添加实验课程</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="source">
          <el-table-column label="实验编码" prop="experimentCode"></el-table-column>
          <el-table-column label="发布教师" prop="username"></el-table-column>
          <el-table-column label="实验名称" prop="experimentName"></el-table-column>
          <el-table-column label="实验课程" prop="courseName"></el-table-column>
          <el-table-column label="实验开始时间">
            <template slot-scope="scope">{{parseDate(scope.row.experimentStartTime)}}</template>
          </el-table-column>
          <el-table-column label="实验结束时间">
            <template slot-scope="scope">{{parseDate(scope.row.experimentEndTime)}}</template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button @click="openEdit(scope.row.id)" type="warning" size="mini">修改</el-button>
              <el-button @click="openDelete(scope.row.id)" type="danger" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-pagination
          :page-size="size"
          :total="total"
          :current-page.sync="page"
          @current-change="load()"
        ></el-pagination>
      </el-row>
    </el-card>
    <EditCourseDialog :visible.sync="showEdit" :id="editId"></EditCourseDialog>
    <EditCourseDialog :visible.sync="showAdd"></EditCourseDialog>
    <DeleteCourseDialog :visible.sync="showDelete" :id="deleteId"></DeleteCourseDialog>
  </div>
</template>

<script>
import EditCourseDialog from "../../../components/dialog/EditCourseDialog.vue";
import DeleteCourseDialog from "../../../components/dialog/DeleteCourseDialog.vue";
import CApi from "../../../api/Course.js";
export default {
  data() {
    return {
      source: [],
      page: 1,
      size: 15,
      total: 15,
      showAdd: false,
      showEdit: false,
      showDelete: false,
      editId: null,
      deleteId: null,
      username: "",
      experimentCode: "",
      experimentName: ""
    };
  },
  components: {
    EditCourseDialog,
    DeleteCourseDialog
  },
  // watch:{
  //   showAdd:function(val){
  //     if(val==false){
  //       this.load();
  //     }
  //   }
  // },
  methods: {
    parseDate(ms) {
      let parsed = new Date(ms);
      return `${parsed.getFullYear()}-${parsed.getMonth() +
        1}-${parsed.getDate()} ${parsed.getHours()}:${parsed.getMinutes()}`;
    },
    testStatus(ms) {
      var value = Date.now();
      if (ms > value) {
        return true;
      }
      return false;
    },
    openDelete(id) {
      this.deleteId = id;
      this.showDelete = true;
    },
    openEdit(id) {
      this.showEdit = true;
      this.editId = id;
    },
    loadParams() {
      return {
        pageNum: this.page,
        pageSize: this.size,
        username: this.username,
        experimentCode: this.experimentCode,
        experimentName: this.experimentName
      };
    },
    load() {
      this.axios
        .get(CApi.getSource, { params: this.loadParams() })
        .then(res => {
          if (res.data.status === 0) {
            this.total = res.data.data.total;
            this.source = res.data.data.list;
          }
        });
    },
    search() {
      this.page = 1;
      this.load();
    },
    reset() {
      this.page = 1;
      (this.username = ""),
        (this.experimentCode = ""),
        (this.experimentName = ""),
        this.load();
    }
  },
  mounted() {
    this.load();
  }
};
</script>

<style>
</style>
